import router from "./router";
import store from "./store";

const whiteList = ['/login', '/404']
import { getProfileAPI } from "./api/user";
router.beforeEach(async (to, from, next) => {
    // 跳转的路由对象
    // from：从哪来
    // next：放行next() 重定向next（路径）
    const token = store.state.user.token
    if (token) {
        // 有token
        if (to.path === '/login') {
            next('/')
        } else {
            next()
            await getProfileAPI()
        }
    } else {
        // 无token
        // 访问的是白名单
        if (whiteList.includes(to.path)) {
            next()
        } else {
            next('/login')
        }
    }
    next()
})